package org.goldfish.cookied_chatroom02.model;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.goldfish.cookied_chatroom02.dao.DaoManager;
import org.goldfish.cookied_chatroom02.entity.UserBean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LoginManagerImpl
        extends RegisterManagerImpl
        implements LoginManager {
    @Override
    public RequestDispatcher chat(HttpServletRequest request) {
        return request.getRequestDispatcher("/chat");
    }

    @Override
    @Nullable
    public Boolean userNewToDB(UserBean bean) {
        String SQL = "select name from user where name=? and password=?";
        if (bean == null)
            return null;
        Object[] bindArgs = {bean.getName(), bean.getPassword()};

        List<List<Object>> result = DaoManager.getInstance().query(SQL, bindArgs);
        if (result == null)
            return null;
        if (result.isEmpty())
            return true;
        bean.setName(result.get(0).get(0).toString());
        return false;
    }

    @Override
    public boolean writeUserBeanToRequestAttr(HttpServletRequest request, UserBean userBean) {
        if (userBean == null)
            return false;
        request.setAttribute(NAME, userBean.getName());
        request.setAttribute(PASSWORD, userBean.getPassword());
        return true;
    }

    @Override
    public boolean writeUserBeanToSession(@NotNull HttpServletRequest request, @NotNull UserBean userBean) {
        request.getSession().setAttribute(NAME, userBean.getName());
        request.getSession().setAttribute(PASSWORD, userBean.getPassword());
        return true;
    }

    @NotNull
    @Override
    public UserBean getUserBeanFromPostInputStream(InputStream stream) {
        Map<String, String> postMap = getFormContentMap(stream);
        return validateNameAndPassword(postMap);
    }

    @Override
    public boolean isLoggedIn(HttpSession session, HttpServlet servlet) {
        Object logged_in_sessions_raw = session.getAttribute(LOGGED_IN_SESSIONS);
        if (!(logged_in_sessions_raw instanceof Set<?> logged_in_sessions_set_raw))
            return false;
        if (logged_in_sessions_set_raw.isEmpty())
            return false;
        return logged_in_sessions_set_raw.contains(session.getId());
    }

    @Override
    public boolean addLoggedIn(HttpServlet servlet, HttpServletRequest request) {
        try {
            Object logged_in_session_map_raw = servlet.getServletContext().getAttribute(LOGGED_IN_SESSIONS);
            if (logged_in_session_map_raw == null)
                return false;
            Map<String, String> logged_in_sessions_map = (Map<String, String>) logged_in_session_map_raw;
            String name = request.getSession().getAttribute(NAME).toString();
            if (logged_in_sessions_map.containsKey(name))
                return false;
            String session_id = request.getSession().getId();
            String res = logged_in_sessions_map.put(session_id, name);
            return res == null;
        } catch (Exception classCastException) {
            return false;
        }
    }


}
